﻿using System;
using System.Drawing;
using System.Threading.Tasks;
using CoreShop.App.Units.Interface;
using CoreShop.Library;
using CoreShop.Model;
using FastMember;
using Sunny.UI;
using Sunny.UI.Win32;

namespace CoreShop.App.Member
{
    public partial class frmMemberCardStatus : UIForm
    {
        public corecmsuser member = null;
        public frmMemberCardStatus(corecmsuser _member)
        {
            InitializeComponent();
            member = _member;
            txtVerifyCode.TextBox.GotFocus += txtSendKey_GotFocus;
            uiNumPad1.FillColor = Color.White;
            uiNumPad1.FillColor2 = Color.White;
        }
        private void txtSendKey_GotFocus(object sender, System.EventArgs e)
        {
            uiNumPad1.SetTextBox((UIEdit)sender, UITextBox.UIEditType.String, 0);
        }

        private async void SendOk()
        {
            if (txtVerifyCode.Text.Trim().IsNullOrEmpty())
            {
                ShowInfoTip("验证码未填。");
                return;
            }

            member.gsPosVip.status = rbtNormal.Checked ? (int)GlobalEnumVars.MemberCardStatus.Used : (int)GlobalEnumVars.MemberCardStatus.Loss;
            try
            {
                string canLock = rbtLoss.Checked ? "0":"1"; //是否锁定 0是1 否               
                ReturnModel returnModel = await MemberApiUtil.UpdateCardStatus(member, canLock, txtVerifyCode.Text);

                if (returnModel.Status != ReturnStatus.Success)
                {
                    UIMessageBox.Show($"状态更新失败,{returnModel.MessageOrString}");
                    return;
                }

                ApiReturnModel apiReturnModel = returnModel.MessageOrString.JsonToEntity<ApiReturnModel>();

                if (apiReturnModel.code != 200)
                {
                    UIMessageBox.Show($"状态更新失败,{apiReturnModel.msg}");
                    return;
                }

                UIMessageBox.Show("状态更新成功");

                this.Invoke(new Action(() =>
                {
                    DialogResult = System.Windows.Forms.DialogResult.Yes;
                    Close();
                    Dispose();
                }));
            }
            catch (Exception ex) { }
            finally { }
        }

        private void GsMemberCardStatus17_Shown(object sender, System.EventArgs e)
        {
            txtVerifyCode.Focus();
            txtStatusName.Text = member.gsPosVip.GetStatusName();
            rbtNormal.Checked = member.gsPosVip.status == (int) GlobalEnumVars.MemberCardStatus.Used ? false: true;
            rbtLoss.Checked = !rbtNormal.Checked;
        }

        private void uiMillisecondTimer1_Tick(object sender, EventArgs e)
        {
            this.Invoke(new Action(() =>
            {
                int tm = btnSendSms.Text.ToInt();

                if (--tm > 1)
                {
                    btnSendSms.Text = tm.ToString();
                }
                else
                {
                    btnSendSms.Text = "获取";
                    btnSendSms.Enabled = true;
                    uiMillisecondTimer1.Stop();
                }
            }));
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void uiNumPad1_OKButtonClick(object sender, string text, int intvalue, double doublevalue)
        {
            SendOk();
        }

        private async void btnSendSms_ClickAsync(object sender, EventArgs e)
        {
            try
            {
                ReturnModel returnModel = await MemberApiUtil.SendSMSCode(AppConstVars.sysUser, member);

                if (returnModel.Status != ReturnStatus.Success)
                {
                    UIMessageBox.Show($"发送验证码失败,{returnModel.MessageOrString}");
                    return;
                }

                ApiReturnModel apiReturnModel = returnModel.MessageOrString.JsonToEntity<ApiReturnModel>();

                if (apiReturnModel.code != 200)
                {
                    UIMessageBox.Show($"发送验证码失败,{apiReturnModel.msg}");
                    return;
                }

                btnSendSms.Invoke(new Action(() =>
                {
                    btnSendSms.Text = "30";
                    btnSendSms.Enabled = false;
                }));

                uiMillisecondTimer1.Start();
                UIMessageBox.Show("发送成功");
            }
            catch { }
            finally { }
        }
    }
}
